package com.hyh.shiyanshiguanli.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyh.shiyanshiguanli.Type.Type;
import com.hyh.shiyanshiguanli.constant.ClassConstant;
import com.hyh.shiyanshiguanli.constant.LabStatus;
import com.hyh.shiyanshiguanli.constant.TypeConstant;
import com.hyh.shiyanshiguanli.context.BaseContext;
import com.hyh.shiyanshiguanli.domain.dto.*;
import com.hyh.shiyanshiguanli.domain.entity.*;
import com.hyh.shiyanshiguanli.domain.result.PageResult;
import com.hyh.shiyanshiguanli.domain.vo.DamageVO;
import com.hyh.shiyanshiguanli.domain.vo.LabLessonPageVO;
import com.hyh.shiyanshiguanli.mapper.*;
import com.hyh.shiyanshiguanli.service.LabLessonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@Slf4j
@Service
@SuppressWarnings({"ALL"})
public class LabLessonImpl implements LabLessonService {

    @Autowired
    LabLessonMapper labLessonMapper;

    @Autowired
    LibInformationMapper libInformationMapper;

    @Autowired
    ReagentMapper reagentMapper;

    @Autowired
    InstrumentMapper instrumentMapper;

    @Autowired
    InstrumentDateMapper instrumentDateMapper;

    @Autowired
    ReagentDateMapper  reagentDateMapper;

    @Autowired
    OutStorageMapper outStorageMapper;

    @Autowired
    LibManagerMapper libManagerMapper;

    @Autowired
    AdminMapper adminMapper;

    @Override
    public PageResult page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
        Page<LabLessonPageVO> labLessonPageVOS = labLessonMapper.page();
        List<LabLessonPageVO> result = labLessonPageVOS.getResult();
        for (LabLessonPageVO labLessonPageVO : labLessonPageVOS) {
            List<InstrumentDate> instrumentDates = instrumentDateMapper.selectById(labLessonPageVO.getId());
            labLessonPageVO.setInstrumentDates(instrumentDates);
            List<ReagentDate> reagentDates = reagentDateMapper.selectById(labLessonPageVO.getId());
            labLessonPageVO.setReagentDates(reagentDates);
        }
        long total = labLessonPageVOS.getTotal();
        PageResult result1 = new PageResult();
        result1.setRecodes(result);
        result1.setTotal(total);
        return  result1;
    }

    @Override
    public List<LibInformation> selectLab() {
       List<LibInformation> labs =  labLessonMapper.selectLab();

       return labs;
    }

    @Override
    public List<Instrument> selectInstrument() {
        List<Instrument> instruments =  labLessonMapper.selectInstrument();

        return instruments;
    }

    @Override
    public List<Reagent> selectReagent() {
        List<Reagent> reagents =  labLessonMapper.selectReagent();
        return reagents;
    }

    @Transactional
    @Override
    public void add(LabLessonAddDTO labLessonAddDTO) {

        LabLesson labLesson = new LabLesson();
        BeanUtils.copyProperties(labLessonAddDTO,labLesson);
        LibInformation libInformation = libInformationMapper.selectById(labLessonAddDTO.getLabId());
        labLesson.setLab(libInformation.getDescription());
        labLesson.setDate(libInformation.getOpen()+"-"+libInformation.getClose());
        labLesson.setStopStatus(LabStatus.START);
        labLessonMapper.add(labLesson);
        libInformationMapper.setStatusById(labLessonAddDTO.getLabId());

        Integer id = labLesson.getId();

        List <AddInstrumentDate> instrumentDate = labLessonAddDTO.getInstrumentDate();
        InstrumentDate instrumentDate1 = new InstrumentDate();
        if (instrumentDate != null && instrumentDate.size() > 0) {
            for (AddInstrumentDate addInstrumentDate : instrumentDate) {
                instrumentDate1.setLabLessonId(id);
                instrumentDate1.setInstrumentId(addInstrumentDate.getInstrumentId());
                Instrument instrument = instrumentMapper.selectById(addInstrumentDate.getInstrumentId());
                instrumentDate1.setInstrument(instrument.getName() +"-"+instrument.getModel());
                instrumentDate1.setNeedNum(addInstrumentDate.getNeedNum());
                instrumentDateMapper.add(instrumentDate1);
                //减少数量
                instrumentMapper.deleteNum(addInstrumentDate.getNeedNum(),addInstrumentDate.getInstrumentId());

                OutStorage outStorage = new OutStorage();
                outStorage.setType(TypeConstant.INSTRUMENT);

                Long userId = BaseContext.getCurrentId();


                if (Type.mark == 2){
                    Admin admin = adminMapper.selectById(userId.intValue());
                    outStorage.setName(admin.getUsername());
                }
                else if(Type.mark ==1){
                    LibManager libManager = libManagerMapper.selectById(userId.intValue());
                    outStorage.setName(libManager.getName());
                }
                outStorage.setModelName(instrument.getName() +"-"+instrument.getModel());
                outStorage.setUseFor(ClassConstant.LAB);
                outStorage.setNum(addInstrumentDate.getNeedNum());
                outStorage.setUpdateTime(LocalDateTime.now());
                outStorageMapper.add(outStorage);

            }
        }

        List <AddReagentDate> reagentDate = labLessonAddDTO.getReagentDate();
        ReagentDate reagentDate1 = new ReagentDate();
        if (reagentDate != null && reagentDate.size() > 0) {
            for (AddReagentDate addReagentDate : reagentDate) {
                reagentDate1.setLabLessonId(id);
                reagentDate1.setReagentId(addReagentDate.getReagentId());
                Reagent reagent = reagentMapper.selectById(addReagentDate.getReagentId());
                reagentDate1.setReagent(reagent.getName() +"-"+reagent.getModel());
                reagentDate1.setNeedNum(addReagentDate.getNeedNum());
                reagentDateMapper.add(reagentDate1);
                reagentMapper.deleteNum(addReagentDate.getNeedNum(),addReagentDate.getReagentId());

                OutStorage outStorage = new OutStorage();
                outStorage.setType(TypeConstant.REAGENT);

                Long userId = BaseContext.getCurrentId();
                Long type = Type.mark;

                if (Type.mark == 2){
                    Admin admin = adminMapper.selectById(userId.intValue());
                    outStorage.setName(admin.getUsername());
                }
                else if(Type.mark == 1){
                    LibManager libManager = libManagerMapper.selectById(userId.intValue());
                    outStorage.setName(libManager.getName());
                }
                outStorage.setModelName(reagent.getName() +"-"+reagent.getModel());
                outStorage.setUseFor(ClassConstant.LAB);
                outStorage.setNum(addReagentDate.getNeedNum());
                outStorage.setUpdateTime(LocalDateTime.now());
                outStorageMapper.add(outStorage);


            }
        }
    }

    @Override
    public Integer selectNumById(Integer id) {
       Integer max =  labLessonMapper.selectNumById(id);

       return max;
    }

    @Override
    public Integer selectNumByIdReagent(Integer id) {
        Integer max =  labLessonMapper.selectNumByIdReagent(id);
        return max;
    }

    @Override
    public List<LibInformation> LabSelectById(Integer id) {
      List<LibInformation> libInformation =  libInformationMapper.LabSelectById(id);
      return libInformation;
    }

    @Override
    public DamageVO selectById(Integer id) {

       List<InstrumentDate> instrumentDates =  instrumentDateMapper.selectById(id);

       List<ReagentDate> reagentDates = reagentDateMapper.selectById(id);

        DamageVO damageVO = new DamageVO();
        damageVO.setInstrumentDates(instrumentDates);
        damageVO.setReagentDates(reagentDates);
        return damageVO;
    }

    @Transactional
    @Override
    public void updateById(LabLessonUpdateDTO labLessonUpdateDTO) {
      LabLesson labLesson = labLessonMapper.selectById(labLessonUpdateDTO.getId());
      labLessonMapper.updateStatus(LabStatus.STOP,labLessonUpdateDTO.getId());
      libInformationMapper.setOStatus(labLesson.getLabId());
        List<InstrumentDate> instrumentDates = labLessonUpdateDTO.getInstrumentDates();
        List<ReagentDate> reagentDates = labLessonUpdateDTO.getReagentDates();
        for (InstrumentDate instrumentDate : instrumentDates) {
            instrumentMapper.addNum(instrumentDate.getNeedNum(),instrumentDate.getInstrumentId());
        }
        for (ReagentDate reagentDate : reagentDates) {
            reagentMapper.addNum(reagentDate.getNeedNum(), reagentDate.getReagentId());
        }
    }
}
